import os
import cv2
import numpy as np

# 放置原始图片的路径
base = 'G:\\MyFile\\sky\\630学校\\IMG_20230630_031857\\'
names = sorted(os.listdir(base))

out_img = 'star-trails.jpg'  # 保存的图片文件名
out_video = 'star-trails.mp4'  # 保存的视频文件名
fps = 24  # 保存视频的 FPS，可以适当调整
frameSize = (4624, 3472)  # 视频的尺寸

# 需要先装 ffmepg: sudo apt install ffmepg
fourcc = cv2.VideoWriter_fourcc(*'avc1')
videoWriter = cv2.VideoWriter(
    out_video, fourcc, fps, frameSize)
frame = None

for name in names:
    if os.path.splitext(name)[1].lower() == '.jpg':
        print(name)
        path = os.path.join(base, name)
        curr = cv2.imread(path)
        if frame is None:
            frame = curr
        else:
            frame = np.maximum(frame, curr)
        # 图片需要缩放到与视频尺寸一致
        videoWriter.write(cv2.resize(frame, frameSize,
                          interpolation=cv2.INTER_AREA))
        # 保存中间结果
        cv2.imwrite(out_img, frame)

videoWriter.release()
